home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / a_utils / _archvrs / unix / unzip51 / unix.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-04  |  5.7 KB  |  164 lines

  1. /*---------------------------------------------------------------------------
  2.  
  3.   unix.c
  4.  
  5.   Unix-specific routines for use with Info-ZIP's UnZip 5.1 and later.
  6.  
  7.   ---------------------------------------------------------------------------*/
  8.  
  9.  
  10. #include "unzip.h"
  11.  
  12.  
  13. /**********************/
  14. /* Function mapattr() */
  15. /**********************/
  16.  
  17. int mapattr()
  18. {
  19.     ulg  tmp = crec.external_file_attributes;
  20.  
  21.     switch (pInfo->hostnum) {
  22.         case UNIX_:
  23.         case VMS_:
  24.             pInfo->file_attr = (unsigned)(tmp >> 16);
  25.             return 0;
  26.         case AMIGA_:
  27.             tmp = (unsigned)(tmp>>17 & 7);   /* Amiga RWE bits */
  28.             pInfo->file_attr = (unsigned)(tmp<<6 | tmp<<3 | tmp);
  29.             break;
  30.         /* all remaining cases:  expand MSDOS read-only bit into write perms */
  31.         case FS_FAT_:
  32.         case FS_HPFS_:
  33.         case FS_NTFS_:
  34.         case MAC_:
  35.         case ATARI_:             /* (used to set = 0666) */
  36.         case TOPS20_:
  37.         default:
  38.             tmp = !(tmp & 1) << 1;   /* read-only bit --> write perms bits */
  39.             pInfo->file_attr = (unsigned)(0444 | tmp<<6 | tmp<<3 | tmp);
  40.             break;
  41.     } /* end switch (host-OS-created-by) */
  42.  
  43.     /* for originating systems with no concept of "group," "other," "system": */
  44.     umask( (int)(tmp=umask(0)) );    /* apply mask to expanded r/w(/x) perms */
  45.     pInfo->file_attr &= ~tmp;
  46.  
  47.     return 0;
  48.  
  49. } /* end function mapattr() */
  50.  
  51.  
  52.  
  53.  
  54.  
  55. /**************************************/
  56. /* Function set_file_time_and_close() */
  57. /**************************************/
  58.  
  59. void set_file_time_and_close()
  60. {
  61.     static short yday[]={0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
  62.     long m_time;
  63.     int yr, mo, dy, hh, mm, ss, leap, days=0;
  64.     struct utimbuf tp;
  65. #   define YRBASE  1970
  66. #ifndef __386BSD__
  67. #ifdef BSD
  68.     static struct timeb tbp;
  69. #else /* !BSD */
  70.     extern long timezone;
  71. #endif /* ?BSD */
  72. #endif /* !__386BSD__ */
  73.  
  74.  
  75. /*---------------------------------------------------------------------------
  76.     If symbolic links are supported, allocate a storage area, put the uncom-
  77.     pressed "data" in it, and create the link.  Since we know it's a symbolic
  78.     link to start with, we shouldn't have to worry about overflowing unsigned
  79.     ints with unsigned longs.
  80.   ---------------------------------------------------------------------------*/
  81.  
  82. #ifdef SYMLINKS
  83.     if (symlnk) {
  84.         unsigned ucsize = (unsigned)lrec.ucsize;
  85.         char *linktarget = (char *)malloc((unsigned)lrec.ucsize+1);
  86.  
  87.         close(outfd);                       /* close "data" file... */
  88.         outfd = open(filename, O_RDONLY);   /* ...and reopen for reading */
  89.         if (!linktarget || (read(outfd, linktarget, ucsize) != ucsize)) {
  90.             fprintf(stderr, "\nwarning:  symbolic link (%s) failed\n",
  91.               filename);
  92.             if (linktarget)
  93.                 free(linktarget);
  94.             close(outfd);
  95.             return;
  96.         }
  97.         close(outfd);                       /* close "data" file for good... */
  98.         unlink(filename);                   /* ...and delete it */
  99.         linktarget[ucsize] = '\0';
  100.         fprintf(stdout, "-> %s ", linktarget);
  101.         if (symlink(linktarget, filename))  /* create the real link */
  102.             perror("symlink error");
  103.         free(linktarget);
  104.         return;                             /* can't set time on symlinks */
  105.     }
  106. #endif /* SYMLINKS */
  107.  
  108.     close(outfd);
  109.     if (cflag)          /* can't set time on stdout */
  110.         return;
  111.  
  112. /*---------------------------------------------------------------------------
  113.     Convert from MSDOS-format local time and date to Unix-format 32-bit GMT
  114.     time:  adjust base year from 1980 to 1970, do usual conversions from
  115.     yy/mm/dd hh:mm:ss to elapsed seconds, and account for timezone and day-
  116.     light savings time differences.
  117.   ---------------------------------------------------------------------------*/
  118.  
  119.     yr = ((lrec.last_mod_file_date >> 9) & 0x7f) + (1980 - YRBASE);
  120.     mo = ((lrec.last_mod_file_date >> 5) & 0x0f) - 1;
  121.     dy = (lrec.last_mod_file_date & 0x1f) - 1;
  122.     hh = (lrec.last_mod_file_time >> 11) & 0x1f;
  123.     mm = (lrec.last_mod_file_time >> 5) & 0x3f;
  124.     ss = (lrec.last_mod_file_time & 0x1f) * 2;
  125.  
  126.     /* leap = # of leap yrs from YRBASE up to but not including current year */
  127.     leap = ((yr + YRBASE - 1) / 4);   /* leap year base factor */
  128.  
  129.     /* how many days from YRBASE to this year? (& add expired days this year) */
  130.     days = (yr * 365) + (leap - 492) + yday[mo];
  131.  
  132.     /* if year is a leap year and month is after February, add another day */
  133.     if ((mo > 1) && ((yr+YRBASE)%4 == 0) && ((yr+YRBASE) != 2100))
  134.         ++days;   /* OK through 2199 */
  135.  
  136.     /* convert date & time to seconds relative to 00:00:00, 01/01/YRBASE */
  137.     m_time = ((days + dy) * 86400) + (hh * 3600) + (mm * 60) + ss;
  138.  
  139.     /* adjust for local timezone */
  140. #ifdef BSD
  141. #ifdef __386BSD__
  142.     m_time -= localtime(&m_time)->tm_gmtoff;  /* seconds EAST of GMT:  subtr. */
  143. #else /* !__386BSD__ */
  144.     ftime(&tbp);                    /* get `timezone' */
  145.     m_time += tbp.timezone * 60L;   /* seconds WEST of GMT:  add */
  146. #endif /* ?__386BSD__ */
  147. #else /* !BSD */
  148.     tzset();              /* get `timezone' */
  149.     m_time += timezone;   /* seconds WEST of GMT:  add */
  150. #endif /* ?BSD */
  151.  
  152.     /* adjust for daylight savings time (or local equivalent) */
  153. #ifndef __386BSD__  /* (DST already added to tm_gmtoff, so skip tm_isdst) */
  154.     if (localtime(&m_time)->tm_isdst)
  155.         m_time -= 60L * 60L;   /* adjust for daylight savings time */
  156. #endif
  157.  
  158.     /* set the file's access and modification times */
  159.     tp.actime = tp.modtime = m_time;
  160.     if (utime(filename, &tp))
  161.         fprintf(stderr, "warning:  can't set the time for %s\n", filename);
  162.  
  163. } /* end function set_file_time_and_close() */
  164.